package com.chinatsp.onlineapp.ui.news;


import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import tsp.cn.http.tools.T;

import com.chinatsp.onlineapp.R;
import com.chinatsp.onlineapp.bean.NewsChannel;
import com.chinatsp.onlineapp.bean.NewsDetail;
import com.chinatsp.onlineapp.listener.DetailNewsListener;
import com.chinatsp.onlineapp.presenter.NewsChannelDao;


public class NewsMainActivity extends com.chinatsp.onlineapp.base.BaseActivity implements OnItemClickListener, DetailNewsListener {
	 ListView lv_channel;//新闻分类
     FragmentManager fm;
     FragmentTransaction ft;
     NewsListFragment listFragment;
    // FragmentNewsTitle titleFragment;
     NewsDetailFragment detailFragment;
     NewsChannelDao dao;//获取新闻分类的 类 

     ImageView iv_back;//返回按钮

     //新闻频道 适配
     NewsColumnAdapter newsColumnAdapter;


     List<NewsChannel> channelList=new ArrayList<NewsChannel>();//新闻分类

     int currentPosition=0;//新闻频道的选取;
     FrameLayout frameLayout;
	
	 @Override
		public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_all_news);
	        initView();
	        initListener();
	        initData();
	    }
	 
	 private void initListener() {
		 lv_channel.setOnItemClickListener(this);
		 iv_back.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				onClickBack();
			}
		});
		
	}
	 
	 //处理新闻返回逻辑
	 void onClickBack(){
		 if(detailFragment!=null&&detailFragment.isVisible()){
			 addFramentToTitle();
		 }else{
			 finish();
		 }
	 }
	@Override
	    public void onRequestSuccess(int requestCode) {
	        super.onRequestSuccess(requestCode);
	        // 开始更新UI
	        showProgress(false);
	        channelList=dao.getChannelList().getResp_data();	        
	        newsColumnAdapter.setList(channelList);
	        newsColumnAdapter.notifyDataSetChanged();
	        
	      //添加 碎片
            addFramentToTitle();
            //获取完了 新闻 继续 获取标题列表
	    }
	
	private void addFramentToTitle() {
		/*titleFragment=new FragmentNewsTitle();
        titleFragment.setItemListener(this);
        Bundle bundle=new Bundle();
        bundle.putString("channel_id", channelList.get(currentPosition).getChannel_id());
        titleFragment.setArguments(bundle);
        fm.beginTransaction().replace(R.id.frame,titleFragment).commit();*/
		listFragment=new NewsListFragment();
		listFragment.setItemListener(this);
        Bundle bundle=new Bundle();
        bundle.putString("channel_id", channelList.get(currentPosition).getChannel_id());
        listFragment.setArguments(bundle);
        fm.beginTransaction().replace(R.id.frame,listFragment).commit();
		
	}

		@Override
	    public void onRequestFailed(String errorNo, String errorMessage) {
	        super.onRequestFailed(errorNo, errorMessage);
	        // 开始更新UI
	        showProgress(false);
	        T.showShort(NewsMainActivity.this, "网络异常,请重新加载");
	        finish();
	        
	    }
	 

	private void initData() {
		 fm=getSupportFragmentManager();
		 newsColumnAdapter=new NewsColumnAdapter(this);
         newsColumnAdapter.setCurrentPosition(0);
         newsColumnAdapter.setList(channelList);
         lv_channel.setAdapter(newsColumnAdapter);
         dao=new NewsChannelDao(this, this);
         
         showProgress(true);
         dao.sendUrl();//开始获取数据
		
	}

	private void initView() {
		
         lv_channel= (ListView) findViewById(R.id.lv_channel);
         frameLayout= (FrameLayout) findViewById(R.id.frame);
         iv_back= (ImageView) findViewById(R.id.iv_back);
         

	}
	 @Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       //切换新闻频道 改变布局
		 newsColumnAdapter.setCurrentPosition(position);
         newsColumnAdapter.notifyDataSetChanged();
         currentPosition=position;//新闻频道 id
         //切换 新闻标题
         /*if(titleFragment==null){
        	 addFramentToTitle();
         }else{
        	 titleFragment.setId(channelList.get(position).getChannel_id());
        	 titleFragment.changeNewsChannel();
         }*/
         addFramentToTitle();
         
     }

	 
	@Override
	public void itemClick(NewsDetail newsDetail) {
		 	detailFragment=new NewsDetailFragment();

	        Bundle bundle=new Bundle();
	        bundle.putString("news_title", newsDetail.getNews_title());
	        bundle.putString("news_content",newsDetail.getNews_content());
	        detailFragment.setArguments(bundle);
	        detailFragment.setRefresh(this);
	        fm.beginTransaction().replace(R.id.frame,detailFragment).commit();
		
	}
	
	
	//点击刷新 新闻 
	@Override
	public void refresh() {
		
		if(listFragment==null){
			listFragment=new NewsListFragment();
			listFragment.setItemListener(this);
            Bundle bundle=new Bundle();
            bundle.putString("channel_id", channelList.get(currentPosition).getChannel_id());
            listFragment.setArguments(bundle);
            fm.beginTransaction().replace(R.id.frame,listFragment).commit();
        }else{
            fm.beginTransaction().replace(R.id.frame,listFragment).commit();
        }
	}

}
